import { http } from "@/utils/http";

/**
 * 批量更新第三方服务提供商价格
 * 用于一次性更新多个服务的价格配置
 * @param data 数组格式，例如：[{ standardCode: 'tg_activate', provider: 'Apifox API提供商', price: 100 }, ...]
 */
export const updatePricesBatch = (data: any[]) => {
  return http.request<any>(
    "put",
    "/api/admin/providers/services/prices/batch",
    { data }
  );
};

/**
 * 更新指定服务的第三方服务提供商价格
 * @param params.standardCode 服务标准代码，例如 "tg_activate"
 * @param params.provider API 提供商名称（枚举："Apifox API提供商"、"TNT-PUB API提供商"）
 * @param params.costPrice 价格（积分）
 */
export const updateServicePrice = (params: {
  standardCode: string;
  provider: string;
  costPrice: number;
}) => {
  return http.request<any>("put", "/api/admin/providers/services/cost-price", {
    params
  });
};

/**
 * 切换指定服务的第三方服务提供商激活状态
 * @param params.standardCode 服务标准代码，例如 "tg_activate"
 * @param params.provider API 提供商名称（枚举："Apifox API提供商"、"TNT-PUB API提供商"）
 * @param params.active 是否激活（true 启用, false 禁用）
 */
export const updateActiveStatus = (params: {
  standardCode: string;
  provider: string;
  active: boolean;
}) => {
  return http.request<any>(
    "put",
    "/api/admin/providers/services/active-status",
    { params }
  );
};

/**
 * 同步指定第三方服务配置
 * 从指定 API 提供商同步最新的服务配置
 * @param provider API 提供商名称，例如 "Apifox API提供商" 或 "TNT-PUB API提供商"
 */
export const syncServices = (params: { provider: string }) => {
  return http.request<any>("post", `/api/admin/providers/services/sync`, {
    params
  });
};

/**
 * 重置所有服务的初始同步标志
 * 将所有服务的初始同步状态重置为已完成初始同步状态
 */
export const resetInitialSynced = () => {
  return http.request<any>(
    "post",
    "/api/admin/providers/services/reset-initial-synced"
  );
};

/**
 * 获取指定服务的所有提供商配置
 * @param standardCode 服务标准代码，例如 "tg_activate"
 */
export const getProvidersByService = (standardCode: string) => {
  return http.request<any>(
    "get",
    `/api/admin/providers/services/service/${standardCode}`
  );
};

/**
 * 按提供商获取服务配置
 * 获取指定提供商的所有服务配置，不传provider则获取所有提供商配置
 */
export const getAllServiceProviders = (params: {
  provider?: string;
  onlyActive?: boolean | string;
}) => {
  return http.request<any>("get", "/api/admin/providers/services/search", {
    params
  });
};

/**
 * 根据提供商名称获取服务配置
 * @param provider API 提供商名称，例如 "Apifox API提供商"
 * @param onlyActive 是否只返回激活状态的服务（可选）
 */
export const getServicesByProvider = (
  provider: string,
  onlyActive?: boolean
) => {
  return http.request<any>(
    "get",
    `/api/admin/providers/services/provider/${provider}`,
    {
      params: { onlyActive }
    }
  );
};

/**
 * 获取指定服务和提供商的配置详情
 * @param params.standardCode 服务标准代码
 * @param params.provider API 提供商名称
 */
export const getServiceProviderDetail = (params: {
  standardCode: string;
  provider: string;
}) => {
  return http.request<any>("get", "/api/admin/providers/services/detail", {
    params
  });
};

// 创建服务配置/api/admin/providers/services
export const createServiceConfig = (data: {
  standardCode: string;
  apiProvider: string;
  adapterType: string;
  taskType: string;
  apiName: string;
  costScore: string;
  isActive: boolean;
  remark: string;
  name: string;
  image: string;
}) => {
  return http.request<any>("post", "/api/admin/providers/services", {
    data
  });
};
// 更新服务配置/api/admin/providers/services/{id}
export const updateServiceConfig = (
  id: string,
  data: {
    standardCode: string;
    apiProvider: string;
    adapterType: string;
    taskType: string;
    apiName: string;
    costScore: string;
    isActive: boolean;
    remark: string;
    name: string;
    image: string;
  }
) => {
  return http.request<any>("put", `/api/admin/providers/services/${id}`, {
    data
  });
};
// 删除服务配置/api/admin/providers/services/{id}
export const deleteServiceConfig = (id: string) => {
  return http.request<any>("delete", `/api/admin/providers/services/${id}`);
};

// 设置第三方服务最小-最大行数
export const updateServiceLineLimits = (
  id: string,
  data: {
    minLineCount: number;
    maxLineCount: number;
  }
) => {
  return http.request<any>(
    "put",
    `/api/admin/providers/services/${id}/line-limits`,
    {
      params: data
    }
  );
};

// 查询可见性的映射关系
export const getColumnVisibility = (id: string) => {
  return http.request<any>(
    "get",
    `api/admin/providers/services/${id}/column-visibility`
  );
};
// 设置开关列可见性
export const updateColumnVisibilityEnabled = (
  id: string,
  data: {
    enabled: boolean;
  }
) => {
  return http.request<any>(
    "put",
    `/api/admin/providers/services/${id}/column-visibility/enabled`,
    {
      data,
      headers: { "Content-Type": "application/x-www-form-urlencoded" }
    }
  );
};
// 设置可见列映射(覆盖)
export const replaceVisibleColumnIds = (
  id: string,
  data: {
    mappingIds?: number[] | string[];
  }
) => {
  return http.request<any>(
    "put",
    `/api/admin/providers/services/${id}/column-visibility/column-ids`,
    {
      data
    }
  );
};
